<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebStorge</title>
</head>
<body>
<div id="result"></div>
<script>
    function getCount()
    {
        if(typeof(Storage)!=="undefined")
        {
            if(localStorage.clickcount)
                localStorage.clickcount=Number(localStorage.clickcount)+1;
            else
                localStorage.clickcount=1;
            document.getElementById("result").innerHTML="已经点击了"+localStorage.clickcount;
        }
        else
        {
            document.getElementById("result").innerHTML="Sorry";
        }

    }
    function getCount_session()
    {
        if(typeof(Storage)!=="undefined")
        {
            if(sessionStorage.clickcount)
                sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
            else
                sessionStorage.clickcount=1;
            document.getElementById("result").innerHTML="已经点击了"+sessionStorage.clickcount;
        }
        else
        {
            document.getElementById("result").innerHTML="Sorry";
        }

    }
    function getItem()
    {
        var result="";
        var content=document.getElementById("result");
        if(localStorage.length>0){
            result="all:"+localStorage.length+'\r\n'
            for(var i=0;i<localStorage.length;i++){
            var key = localStorage.key(i);
            var value = localStorage.getItem(key);
            result+= key+":"+value;
        }

            content.innerHTML = result+"<br/>";
        }else{
            content.innerHTML = "数据为空……";
        }
    }
    function getclear()
    {
        var content=document.getElementById("result");
        if(localStorage.length>0){
           localStorage.clear();
            content.innerHTML = "已清除……";
        }else{
            content.innerHTML = "数据为空……";
        }
    }

</script>
<p>
    <button type="button" onclick="getCount()">点击增加次数</button>
    <button type="button" onclick="getCount_session()">点击增加次数_session</button>
    <button type="button" onclick="getItem()">获取本地变量</button>
    <!--<button type="button" onclick="getclear()">清除</button>-->
    <input type="button" onclick="getclear()">清除
</p>
</body>
</html>